﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace MainWebService
{
    public class ServicesForData
    {

        internal string SaveToDevice(int serviceId, int userId, int clientId, int hashCode)
        {
            return "";
        }

        public FileObject GetFile(string fileName)
        {
            FileObject fileObj = new FileObject();

            if (File.Exists(fileName))
            {
                //note that i'm currently reading the file from the location of the currently executing assembly .. not sure where that is .. so u could specify your explicit location
                byte[] bt = File.ReadAllBytes(fileName);
                fileObj.fileBytes = bt;
                fileObj.fileExtension = Path.GetExtension(fileName);
                return fileObj;
            }
            else
            {
                return null; //or however you choose to handle FileNotFound
            }
        }
        public void SendFile(FileObject fileObject)
        {
            if (fileObject != null)
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=Export.csv");
                HttpContext.Current.Response.OutputStream.Write(fileObject.fileBytes, 0, fileObject.fileBytes.Length);
                HttpContext.Current.Response.End();

            }
        }
        internal string SaveFromDevice(int serviceId, int userId, int clientId, int hashCode)
        {
            throw new NotImplementedException();
        }

        internal string GetScanDataCSV(int serviceId, int userId, int clientId, int hashCode)
        {
            StringBuilder result = new StringBuilder();
            try
            {

                DAO d = new DAO();
                DataSet ds = new DataSet();
                List<SqlParameter> param = new List<SqlParameter>();
                param.Add(d.AddParameter("@ServiceId", serviceId));
                param.Add(d.AddParameter("@UserId", userId));
                param.Add(d.AddParameter("@ClientId", clientId));
                param.Add(d.AddParameter("@HashCode", hashCode));
                ds = d.ExecuteDataSet("P_GetScanDataCSV", param);
                if (Utility.IsValidDataSet(ds))
                {

                    for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                    {
                        result.Append(ds.Tables[0].Columns[i].ColumnName);
                        result.Append(i == ds.Tables[0].Columns.Count - 1 ? "\n" : ",");
                    }

                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                        {
                            result.Append(row[i].ToString());
                            result.Append(i == ds.Tables[0].Columns.Count - 1 ? "\n" : ",");
                        }
                    }

                }
            }
            catch (Exception ex)
            {

            }
            return result.ToString();
        }

        internal string AddData(int serviceId, int userId, int clientId, int hashCode, ScanDataFile _sData)
        {
            object messgae = null;
            try
            {
                DAO d = new DAO();
                List<SqlParameter> param = new List<SqlParameter>();
                param.Add(d.AddParameter("@ServiceId", serviceId));
                param.Add(d.AddParameter("@UserId", userId));
                param.Add(d.AddParameter("@ClientId", clientId));
                param.Add(d.AddParameter("@HashCode", hashCode));
                param.Add(d.AddParameter("@Value", _sData.Value));
                param.Add(d.AddParameter("@Response", _sData.Response));
                param.Add(d.AddParameter("@Validity", _sData.validity));
                param.Add(d.AddParameter("@Messages", messgae, SqlDbType.VarChar, 500, ParameterDirection.InputOutput));
                d.ExecuteQuery("P_CreateScanData", param, ref messgae);
            }
            catch (Exception ex)
            {
                messgae = ex.Message.ToString();
            }
            return messgae.ToString();
        }
    }
   

    public class ScanDataFile
    {
        public string Value
        {
            get;
            set;
        }
        public string Response { get; set; }
        public string validity { get; set; }

    }
}